import { ObjCreate } from "../common/ObjCreate";
import { ImagesConst } from "../../resource/ImagesConst";
import { GTextFormat } from "../../../common/GTextFormat";
import { BuffPanel } from "./BuffPanel";
import { FontUtil } from "../../../common/font/FontUtil";
import { FilterConst } from "../../../component/gconst/FilterConst";
import { BaseProgressBar } from "../common/BaseProgressBar";
import { SEntityInfo } from "../../../../Message/BroadCast/SEntityInfo";
import { AvatarInfo } from "../../resource/info/AvatarInfo";
import { ObjectUtils } from "../../../../com/gengine/utils/ObjectUtils";
import { SkillStateConfig } from "../../resource/SkillStateConfig";
import { TState } from "../../../../Message/Db/Tables/TState";
import { BossConfig } from "../../resource/BossConfig";
import { EBossShowType } from "../../../../Message/Public/EBossShowType";
import { GlobalClass } from "../../../../com/mui/core/GlobalClass";
import { LoaderManager } from "../../../../com/gengine/resource/LoaderManager";
import { ImageInfo } from "../../../../com/gengine/resource/info/ImageInfo";
import { GButton } from "../../../../com/mui/controls/GButton";
type int = number;
//class MonsterAvatar
    
    export  class MonsterAvatar /* flash.display.Sprite */
    {
        constructor()
        {
            
            this._buffers = [];
            this.init();
            return;
        }

        /* internal  */init(): void
        {
            this._bg = ObjCreate.createBg(0, 0, 193, 60, this, ImagesConst.MonsterAvatar);
            this._nameTxt = ObjCreate.createTextField("", 66, 7, 122, 20, this, new GTextFormat("", 12, 16777215));
            this._nameTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
            this._levelTxt = ObjCreate.createTextField("", 59, 38, 20, 20, this, new GTextFormat("", 12, 16777215));
            this._avatar = new Object /* flash.display.Bitmap */();
            this._avatar.x = 10;
            this._avatar.y = 4;
            this.addChild(this._avatar);
            this._lifeBar = this.createProgressBars("", ImagesConst.MonsterLifeBar, 0, 0);
            this._lifeBar.setSize(122, 10);
            this._lifeBar.move(66, 27);
            this._lifeBar.showPercentText = true;
            this.addChild(this._lifeBar);
            this._buffPanel = new BuffPanel(15);
            this._buffPanel.x = 87;
            this._buffPanel.y = 45;
            this.addChild(this._buffPanel);
            this._minBtn = ObjCreate.createGButton("", 62, 63, 13, 13, this, "MinMinusBtn");
            this._minBtn.visible = false;
            this._minBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onBuffMinBtnClick);
            return;
        }

        /* internal  */onBuffMinBtnClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._buffPanel.isHideAfterFine) 
            {
                this._buffPanel.showBuffAfterFine();
                this._minBtn.styleName = "MinMinusBtn";
            }
            else 
            {
                this._buffPanel.hideBuffAfterFine();
                this._minBtn.styleName = "MinPlusBtn";
            }
            return;
        }

        /* internal  */showManaBar(arg1: Boolean): void
        {
            if (arg1) 
            {
                if (this._manaBar == null) 
                {
                    this._manaBar = this.createProgressBars("", ImagesConst.BossManaBar, 0, 0);
                    this._manaBar.setSize(171, 7);
                    this._manaBar.move(78, 38);
                    this._manaBar.showPercentText = true;
                }
                if (!this.contains(this._manaBar)) 
                {
                    this.addChild(this._manaBar);
                }
            }
            else if (this._manaBar && this.contains(this._manaBar)) 
            {
                this.removeChild(this._manaBar);
            }
            return;
        }

        /* internal  */showMutiLifeBar(arg1: Boolean): void
        {
            if (arg1) 
            {
                if (this._lifeBar2 == null) 
                {
                    this._lifeBar2 = this.createBossLifeBar(ImagesConst.BossLifeBarPurple);
                    this._lifeBar2.showProgressText = false;
                    this._lifeBar2.showPercentText = false;
                }
                this._lifeBar2.setValue(100, 100);
                if (this._lifeBar3 == null) 
                {
                    this._lifeBar3 = this.createBossLifeBar(ImagesConst.BossLifeBarYellow);
                    this._lifeBar3.showProgressText = false;
                    this._lifeBar3.showPercentText = false;
                }
                this._lifeBar3.setValue(100, 100);
                if (!this.contains(this._lifeBar2)) 
                {
                    this.addChild(this._lifeBar2);
                }
                if (!this.contains(this._lifeBar3)) 
                {
                    this.addChild(this._lifeBar3);
                }
                if (this._percentTxt == null) 
                {
                    this._percentTxt = ObjCreate.createTextField("", this._lifeBar2.x, this._lifeBar2.y - 3, 219, 10, null, new GTextFormat(FontUtil.songtiName, 12, 16580298));
                    this._percentTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
                    this._percentTxt.filters = [FilterConst.glowFilter];
                }
                if (!this.contains(this._percentTxt)) 
                {
                    this.addChild(this._percentTxt);
                }
                this._lifeBar.showProgressText = false;
                this._lifeBar.showPercentText = false;
            }
            else 
            {
                if (this._percentTxt && this.contains(this._percentTxt)) 
                {
                    this.removeChild(this._percentTxt);
                }
                if (this._lifeBar3 && this.contains(this._lifeBar3)) 
                {
                    this.removeChild(this._lifeBar3);
                }
                if (this._lifeBar2 && this.contains(this._lifeBar2)) 
                {
                    this.removeChild(this._lifeBar2);
                }
                this._lifeBar.showProgressText = true;
                this._lifeBar.showPercentText = true;
            }
            return;
        }

        /* internal  */createBossLifeBar(arg1: string): BaseProgressBar
        {
            var loc1=this.createProgressBars("", arg1, 100, 100);
            loc1.setSize(219, 10);
            loc1.move(66, 26);
            loc1.showProgressText = false;
            return loc1;
        }

        /* internal  */createProgressBars(arg1: string, arg2: string, arg3: int, arg4: int): BaseProgressBar
        {
            var loc1;
            (loc1 = new BaseProgressBar("")).progressBarClass = arg2;
            loc1.setValue(arg3, arg4);
            loc1.setLabelPosition(0, -3);
            return loc1;
        }

        public updateAvatarUI(arg1: SEntityInfo): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=0;
            var loc4=null;
            this._entityInfo = arg1;
            if (arg1) 
            {
                this.loadAvatar(AvatarInfo.getBossAvatarName(arg1.code));
                this._nameTxt.text = arg1.name;
                this._levelTxt.text = arg1.level.toString();
                this._buffPanel.updateBufferByTSateIdArray(arg1.buffers);
                loc2 = [];
                loc3 = 0;
                while (loc3 < arg1.buffers.length) 
                {
                    loc1 = ObjectUtils.baseClone(SkillStateConfig.instance.getInfoByName(arg1.buffers[loc3])) as TState;
                    if (loc1 && !(loc1.icon == "")) 
                    {
                        loc2.push(loc1);
                    }
                    ++loc3;
                }
                if (loc2.length > 5) 
                {
                    this._minBtn.visible = true;
                    if (this._minBtn.styleName != "MinMinusBtn") 
                    {
                        this._buffPanel.hideBuffAfterFine();
                    }
                    else 
                    {
                        this._buffPanel.showBuffAfterFine();
                    }
                }
                else 
                {
                    this._minBtn.visible = false;
                    this._buffPanel.hideBuffAfterFine();
                }
                if (loc4 = BossConfig.instance.getInfoByName(arg1.code)) 
                {
                    if (loc4.showType != EBossShowType._EBossShowTypeBoss) 
                    {
                        this._bg.bitmapData = GlobalClass.getBitmapData(ImagesConst.MonsterAvatar);
                        this._lifeBar.setSize(122, 10);
                        this._lifeBar.progressBarClass = ImagesConst.MonsterLifeBar;
                        this.showManaBar(false);
                        this._buffPanel.y = 45;
                        this.showMutiLifeBar(false);
                        this._lifeBar.setValue(arg1.life, arg1.maxLife);
                        this._nameTxt.x = 66;
                        this._nameTxt.width = 122;
                    }
                    else 
                    {
                        if (arg1.maxMana > 0) 
                        {
                            this._bg.bitmapData = GlobalClass.getBitmapData(ImagesConst.BossAvatar1);
                            this.showManaBar(true);
                            this._manaBar.setValue(arg1.mana, arg1.maxMana);
                            this._buffPanel.y = 50;
                        }
                        else 
                        {
                            this._bg.bitmapData = GlobalClass.getBitmapData(ImagesConst.BossAvatar);
                            this.showManaBar(false);
                            this._buffPanel.y = 45;
                        }
                        this._lifeBar.setSize(219, 10);
                        this._lifeBar.progressBarClass = ImagesConst.BossLifeBarRed;
                        this.showMutiLifeBar(true);
                        this.setBossLifeBarValue(arg1.life, arg1.maxLife);
                        this._nameTxt.x = 66;
                        this._nameTxt.width = 220;
                    }
                }
            }
            return;
        }

        /* internal  */setBossLifeBarValue(arg1: number, arg2: number): void
        {
            var loc2=0;
            var loc1=arg2 / 3;
            if (arg2 != 0) 
            {
                if ((loc2 = Math.ceil(arg1 / arg2 * 100)) > 100) 
                {
                    loc2 = 100;
                }
                this._percentTxt.text = loc2 + "%";
            }
            else 
            {
                this._percentTxt.text = "0%";
            }
            if (arg1 > 2 * loc1) 
            {
                this._lifeBar3.setValue(arg1 - 2 * loc1, loc1);
                this._lifeBar2.setValue(100, 100);
                this._lifeBar.setValue(100, 100);
            }
            else if (arg1 > loc1) 
            {
                this._lifeBar3.setValue(0, 0);
                this._lifeBar2.setValue(arg1 - loc1, loc1);
                this._lifeBar.setValue(100, 100);
            }
            else 
            {
                this._lifeBar3.setValue(0, 0);
                this._lifeBar2.setValue(0, 0);
                this._lifeBar.setValue(arg1, loc1);
            }
            return;
        }

        /* internal  */loadAvatar(arg1: string): void
        {
            if (arg1 != this._backAvatarName) 
            {
                this._avatar.bitmapData = null;
                LoaderManager.instance.load(arg1, this.onAvatarLoaded);
                this._backAvatarName = arg1;
            }
            return;
        }

        /* internal  */onAvatarLoaded(arg1: ImageInfo): void
        {
            this._avatar.bitmapData = arg1.bitmapData;
            return;
        }

        /* internal */ /* var */_bg: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_nameTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_levelTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_lifeBar: BaseProgressBar;

        /* internal */ /* var */_lifeBar2: BaseProgressBar;

        /* internal */ /* var */_lifeBar3: BaseProgressBar;

        /* internal */ /* var */_manaBar: BaseProgressBar;

        /* internal */ /* var */_avatar: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_buffPanel: BuffPanel;

        /* internal */ /* var */_percentTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_entityInfo: SEntityInfo;

        /* internal */ /* var */_buffers: Array<any>;

        /* internal */ /* var */_backAvatarName: string;

        /* internal */ /* var */_minBtn: GButton;
    }
